package com.hnevc.acountms.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hnevc.acountms.R;
import com.hnevc.acountms.dao.FlagDao;
import com.hnevc.acountms.model.Tb_flag;

public class FlagManageActivity extends AppCompatActivity {

    private int mId;
    FlagDao flagDao ;

    EditText mEtFlag;
    Button mBtnUpdate;
    Button mBtnDel;
    private Tb_flag mFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flag_manage);
        Intent intent = getIntent();
        mId = intent.getIntExtra("id",0);
        flagDao = new FlagDao();
        mFlag = flagDao.find(mId);
        initView();
        initEvent();

    }

    private void initEvent() {
        mBtnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFlag.setFlag(mEtFlag.getText().toString());
                flagDao.update(mFlag);
            }
        });
        mBtnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flagDao.delete(mFlag.getId());
                Toast.makeText(FlagManageActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        mEtFlag = (EditText) findViewById(R.id.id_et_flag);
        mBtnUpdate = (Button) findViewById(R.id.id_btn_update);
        mBtnDel = (Button) findViewById(R.id.id_btn_del);
        mEtFlag.setText(mFlag.getFlag().toString());
        
    }

    public static  void actionStart(Context context,int id){
        Intent intent = new Intent(context,FlagManageActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }
}
